<?php

namespace app\admin\controller\activity;

use app\common\controller\Backend;
use think\Db;
/**
 * 
 *
 * @icon fa fa-circle-o
 */
class Details extends Backend
{
    
    /**
     * Seckill模型对象
     * @var \app\admin\model\activity\Seckill
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\activity\Seckill;
        $this->view->assign("statusList", $this->model->getStatusList());
        $this->view->assign("typeList", $this->model->getTypeList());
    }
    
    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */
    /**
     * 查看 
     */
    public function index(){
        $ids = input('ids');
        $seckill = Db::name('seckill')->where(['id'=>$ids])->find();
        $seckill_choice=Db::name('seckill_choice a')
        ->join('shopro_goods b','find_in_set(b.id,a.pid)','LEFT')
        // ->join('seckill_regulate c','c.pid=b.id','LEFT')
        ->join('seckill_regulate c','c.choice_id=a.id','LEFT')
        ->where(['a.seckill_id'=>$ids])
        ->group("c.id")
        ->field("b.original_price,b.title,c.*,a.endtime,a.updatetime,b.id,a.id as seckill_choice_id")
        ->select();
        // dump($seckill_choice);die;
        $this->view->assign("seckill", $seckill);
        $this->view->assign("seckill_choice", $seckill_choice);
        return $this->view->fetch();
    }
    public function details()
    {
        $seckill_id = input('seckill_id');
        $shopro_goods = Db::name('shopro_goods')->select();
        $this->view->assign("seckill_id", $seckill_id);
        $this->view->assign("shopro_goods", $shopro_goods);
        return $this->view->fetch();
    }



    public function choice(){
        $choice = input('choice');
        $seckill_id = input('seckill_id');
        $seckill_choice_find = Db::name('seckill_choice')->where(['seckill_id'=>$seckill_id])->find();
        if($seckill_choice_find){
           $seckill_choice_res = Db::name('seckill_choice')->update(['pid' => $choice,'id'=>$seckill_choice_find['id'],'updatetime'=>time()]);
           if($seckill_choice_res){
                $arr = [
                   'state'     => 1,
                   'message'   => "更新成功！"
                ];
            }else{
                $arr = [
                   'state'     => 2,
                   'message'  => "更新失败！"
                ];
            }
            return json($arr);
        }else{
            $data = [
                'pid'=>$choice,
                'seckill_id'=>$seckill_id,
                'endtime'   =>time(),
                'updatetime'=>time()
            ];
            $seckill_choice = Db::name('seckill_choice')->insert($data);
            if($seckill_choice){
                $arr = [
                   'state'     => 3,
                   'message'   => "添加成功！"
                ];
            }else{
                $arr = [
                   'state'     => 4,
                   'message'  => "添加失败！"
                ];
            }
            return json($arr);
        }
        
    }


    public function show(){
        $seckill_id = input('seckill_id');
        $seckill_choice = Db::name('seckill_choice')->where(['seckill_id'=>$seckill_id])->field('pid')->find();
        if(!$seckill_choice){
            $arr = [
               'state'     => 2,
               'message'  => "失败！"
            ];
            return json($arr);
        }
        $seckill_choice_imp = implode($seckill_choice);
        $shopro_goods = Db::table('fa_shopro_goods')
            ->where('find_in_set(id,:seckill_choice_imp)',['seckill_choice_imp'=>$seckill_choice_imp])
            ->select();
        if($shopro_goods){
            $arr = [
               'state'     => 1,
               'shopro_goods'=>$shopro_goods,
               'message'  => "成功！"
            ];
        }else{
            $arr = [
               'state'     => 0,
               'message'  => "失败！"
            ];
        }
        return json($arr);

    }
    /****
     * Class register                   删除
     * @$id[int]                        商品id
     * @$seckill_choice_id[id]          秒杀id
     * @date                            2020/05/14 
     * @package application\admin\controller\Details
    */
    public function del1(){
        $id = input('id');
        $seckill_choice_id = input('seckill_choice_id');
        $seckill_choice = Db::name('seckill_choice')
                        ->where(['id'=>$seckill_choice_id])
                        ->field('pid')
                        ->find();
        $seckill_choice_pid = implode($seckill_choice);
        $replace = str_replace(','.$id,'',$seckill_choice_pid);
        $seckill_choice = Db::name('seckill_choice')
                        ->where(['id'=>$seckill_choice_id])
                        ->update(['pid'=>$replace]);
       if(!$seckill_choice){
          $replace = str_replace($id,'',$seckill_choice_pid);
          $seckill_choice = Db::name('seckill_choice')
                        ->where(['id'=>$seckill_choice_id])
                        ->update(['pid'=>$replace]);
       }
        if($seckill_choice){
            $seckill_choice_del = Db::name('seckill_choice')
                        ->where(['id'=>$seckill_choice_id])
                        ->find();
            if($seckill_choice_del['pid'] == null){
                $seckill_choice = Db::name('seckill_choice')
                        ->where(['id'=>$seckill_choice_id])
                        ->delete();
            }
            $arr = [
               'state'     => 1,
               'message'  => "成功！"
            ];
        }else{
            $arr = [
               'state'     => 0,
               'message'  => "失败！"
            ];
        }
        return json($arr);
    }
    
}
